package com.bnz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.bnz.util.PageResult;
import com.bnz.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service     // 将服务发布到zookeeper注册中心中
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	/**
	 * 1. 查询所有学生
	 * @return
	 */
	@Override
	public List<Student> findAll() {
		//1.1 查询所有学生
		List<Student> students = studentMapper.selectAll();
		//1.2 遍历学生，查询学生关联的班级名称
		for (Student student : students) {
			student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
		}
		//1.3 返回
		return students;
	}

	/**
	 * 2. 分页查询
	 * @param page     当前页
	 * @param pagesize 每页大小
	 * @return
	 */
	@Override
	public PageResult<Student> findByPage(int page, int pagesize) {
		//2.1 开始分页
		PageHelper.startPage(page,pagesize);
		//2.2 开始查询
		//2.2.1 定义查询条件
		Example example = new Example(Student.class);
		// Example.Criteria criteria = example.createCriteria();
		//2.2.2 封装查询条件
		List<Student> students = studentMapper.selectByExample(example);
		for (Student student : students) {
			student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
		}
		//2.3 转换为PageInfo
		PageInfo<Student> pageInfo = new PageInfo<>(students);
		//2.4 返回PageResult对象
		return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
	}

	/**
	 * 3. 条件查询带分页
	 * @param page
	 * @param pagesize
	 * @param studentVo
	 * @return
	 */
	@Override
	public PageResult<Student> search(int page, int pagesize, StudentVo studentVo) {
		//3.1 开始分页
		PageHelper.startPage(page,pagesize);
		//3.2 开始查询
		//3.2.1 定义查询条件
		Example example = new Example(Student.class);
		Example.Criteria criteria = example.createCriteria();

		//3.2.2 封装查询条件
		if(!StringUtils.isEmpty(studentVo.getSname())){
			criteria.andLike("sname","%"+studentVo.getSname() + "%");
		}
		if(!StringUtils.isEmpty(studentVo.getAddr())){
			criteria.andLike("addr","%"+studentVo.getAddr() + "%");
		}
		if(studentVo.getCid() != 0){
			criteria.andEqualTo("cid",studentVo.getCid());
		}
		//3.3.3 进行实例查询
		List<Student> students = studentMapper.selectByExample(example);
		for (Student student : students) {
			student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
		}
		//3.3 转换为PageInfo
		PageInfo<Student> pageInfo = new PageInfo<>(students);
		//3.4 返回PageResult对象
		return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getList(),pageInfo.getPageNum());
	}

	/**
	 * 4. 添加学生
	 * @param student
	 */
	@Override
	public void add(Student student) {
		studentMapper.insert(student);
		System.out.println("sid = " + student.getSid());
	}

	public PageResult<Student> search2(int page, int pagesize, StudentVo studentVo) {
		//3.1 开始分页
		PageHelper.startPage(page,pagesize);
		Example example = Example.builder(Student.class)
					      .select("sid","sname","sex","age","addr","cid")
				          .andWhere(
				          		 Sqls.custom()
				                .andLike("sname","%" + studentVo.getSname() + "%")
						        .andLike("addr","%" + studentVo.getAddr() + "%")
				          )
						  .build();
		if(studentVo.getCid() != 0){
			example.and().andEqualTo("cid",studentVo.getCid());
		}
		List<Student> students = studentMapper.selectByExample(example);
		for (Student student : students) {
			student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
		}
		//3.3 转换为PageInfo
		PageInfo<Student> pageInfo = new PageInfo<>(students);
		//3.4 返回PageResult对象
		return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
	}

	public PageResult<Student> search3(int page, int pagesize, StudentVo studentVo) {
		//3.1 开始分页
		PageHelper.startPage(page,pagesize);
		//3.2 设置条件
		WeekendSqls<Student> studentWeekendSqls = WeekendSqls.<Student>custom()
				.andLike(Student::getSname, "%" + studentVo.getSname() + "%")
				.andLike(Student::getAddr, "%" + studentVo.getAddr() + "%");
		if(studentVo.getCid() != 0){
			studentWeekendSqls.andEqualTo("cid",studentVo.getCid());
		}
		//3.3 定义查询实例
		Example example = Example.builder(Student.class)
				.where(studentWeekendSqls)
				.build();
		//3.4 开始实例查询
		List<Student> students = studentMapper.selectByExample(example);
		for (Student student : students) {
			student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
		}
		//3.5 转换为PageInfo
		PageInfo<Student> pageInfo = new PageInfo<>(students);
		//3.6 返回PageResult对象
		return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
	}

	//4. 修改学生
	@Override
	public void update(Student stud) {
		studentMapper.updateByPrimaryKey(stud);
	}

	@Override
	public void delete(Integer sid) {
		studentMapper.deleteByPrimaryKey(sid);
	}

}
